package com.howie.parnote.service.impl;


import com.howie.parnote.dao.EmailRecordModuleMapper;
import com.howie.parnote.exception.InvalidParameterException;
import com.howie.parnote.jms.sender.ConfigurableMessageSender;
import com.howie.parnote.jms.sender.MessageSender;
import com.howie.parnote.module.EmailRecordModule;
import com.howie.parnote.module.interaction.SignupSharer;
import com.howie.parnote.service.common.ApplicationDefaultServiceImpl;
import com.howie.parnote.dao.LoginInfoModuleMapper;
import com.howie.parnote.module.LoginInfoModule;
import com.howie.parnote.module.SharerModule;
import com.howie.parnote.security.PasswordHash;
import com.howie.parnote.service.SharerService;
import com.howie.parnote.util.DateUtil;
import com.howie.parnote.util.SharerUtil;
import com.howie.parnote.util.security.DESCoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

/**
 * <p>Title: parnote</p>
 * <p>
 * Description: parnote
 * </p>
 *
 * @author huangyan
 *         Created by HY on 2014/12/16 0016.
 * @version v1.0.0
 */
@Service
public class SharerServiceImpl extends ApplicationDefaultServiceImpl<SharerModule> implements SharerService {

	@Autowired
	private LoginInfoModuleMapper loginInfoModuleMapper;
	@Autowired
	private EmailRecordModuleMapper emailRecordModuleMapper;
	@Resource
	private ConfigurableMessageSender confirmEmailMessageSender;

	@Override
	public void insertSharer(SignupSharer signupSharer) throws InvalidParameterException, InvalidKeySpecException, NoSuchAlgorithmException {
		synchronized (this) {
			LoginInfoModule loginInfo = loginInfoModuleMapper.findByEmail(signupSharer.getEmail());
			if (loginInfo != null) {
				throw new InvalidParameterException();
			} else {
				SharerModule sharer = createSharerModule(signupSharer);
				insertModule(sharer);
				loginInfo = createLoginInfoModule(signupSharer, sharer);
				loginInfoModuleMapper.insert(loginInfo);

				confirmEmailMessageSender.setSendObject(loginInfo);
				confirmEmailMessageSender.handleMessage();
			}
		}
	}

	private SharerModule createSharerModule(SignupSharer signupSharer) {
		SharerModule sharer = new SharerModule();
		sharer.setRealName(signupSharer.getRealName());
		sharer.setNickname(signupSharer.getNickname());
		return sharer;
	}

	private LoginInfoModule createLoginInfoModule(SignupSharer signupSharer, SharerModule sharer) throws NoSuchAlgorithmException, InvalidKeySpecException {
		LoginInfoModule loginInfo;
		loginInfo = new LoginInfoModule();
		loginInfo.setSharer(sharer);
		loginInfo.setSharerEmail(signupSharer.getEmail());
		loginInfo.setLoginPassword(PasswordHash.createHash(signupSharer.getPassword()));
		loginInfo.setRegisterTime(DateUtil.getTimestamp());
		loginInfo.setLoginStatus(LoginInfoModule.LoginStatus.NONACTIVATED.getValue());
		return loginInfo;
	}

	@Override
	public void login(String email, String password, HttpSession session) throws Exception {
		LoginInfoModule loginInfo = loginInfoModuleMapper.findByEmail(email);
		if (loginInfo == null) {
			throw new InvalidParameterException();
		} else {
			if (PasswordHash.validatePassword(password, loginInfo.getLoginPassword())) {
				SharerUtil.saveSharer2Session(loginInfo.getSharer(), session);
			} else {
				throw new InvalidParameterException();
			}
		}
	}

	@Override
	public void updateConfirmEmail(String uid, String sid, String eid) throws Exception {
		Integer sharerId = Integer.parseInt(uid);
		Integer sSharerId = DESCoder.getHexDecodeId(sid);
		if (sharerId != null && sSharerId == sharerId) {
			Integer emailId = DESCoder.getHexDecodeId(eid);
			EmailRecordModule emailRecordModule = emailRecordModuleMapper.selectByPrimaryKey(emailId);
			if (emailRecordModule == null || emailRecordModule.getSharerId() != sharerId) {
				throw new InvalidParameterException();
			}
			if (emailRecordModule.getEmailStatus().equals(EmailRecordModule.Status.UNCONFIRMED.getValue())) {
				LoginInfoModule loginInfoModule = loginInfoModuleMapper.selectByPrimaryKey(sharerId);
				loginInfoModule.setLoginStatus(LoginInfoModule.LoginStatus.AVAILABLE.getValue());
				loginInfoModuleMapper.updateByPrimaryKeySelective(loginInfoModule);
				emailRecordModule.setEmailStatus(EmailRecordModule.Status.CONFIRMED.getValue());
				emailRecordModuleMapper.updateByPrimaryKeySelective(emailRecordModule);
			}

		} else {
			throw new InvalidParameterException();
		}
	}
}
